package com.hmkj.core.api.etonepay;

import com.hmkj.core.api.etonepay.utils.*;
import com.hmkj.core.api.etonepay.xml.entry.y2e.Y2e0010Res;
import com.hmkj.core.api.etonepay.xml.entry.y2e.Y2e1010Req;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Random;

/**
 * 易通代付接口
 *
 * @author Vector
 * @create 2017-12-22 15:28
 */
public class EtoneDFUtil {


    static String init() throws Exception {
        Y2e1010Req req = new Y2e1010Req();
        int money = 1;
        String merinsid = genRondomNo(10);
        String ectonPublicKeyStr = CertKeyUtil.getEctonPublicKey(IConstants.ECTON_PUBLIC_CERTFILE);
        System.out.println("ectonPublicKeyStr:" + ectonPublicKeyStr);
        //测试988088888888888

        req.setMerNo("988088888888888");
        req.setPayTm(DateUtil.getLongDate());
        req.setBatchName(DateUtil.formDateToString14(new Date()));
        req.setBatchNo(genRondomNo(8));//26

        //测试0000000380
        req.setBussNo("0000000380");
        req.setProcedureType("1");//1付款方收费 2收款方收费
        //req.setBackUrl("http://192.168.101.116:7001/NetPay/daifuAccept.action");
        req.setBackUrl("http://www.baidu.com");
        req.setMerinsid(merinsid);
        req.setPayType("1");
        req.setBankNo(EctonRSAUtils.encryptStr("6216261000000000018", ectonPublicKeyStr));//易通公钥加密 测试“6216261000000000018”
        req.setRealName(EctonRSAUtils.encryptStr("全渠道", ectonPublicKeyStr));//加密 测试“全渠道”
        req.setBankName("中国工商银行股份有限公司济南山大路支行");//测试“中国工商银行股份有限公司济南山大路支行”
        req.setPayFee(String.valueOf(money));
        req.setTotCnt(String.valueOf(1));
        req.setTotAmt(String.valueOf(money));//金额

        StringBuffer bufParam = new StringBuffer();
        bufParam.append(req.getMerNo()).append("|")
                .append(req.getPayTm()).append("|")
                .append(req.getBatchName()).append("|")
                .append(req.getBatchNo()).append("|")
                .append(req.getBussNo()).append("|")
                .append(req.getProcedureType()).append("|")
                .append(req.getTotCnt()).append("|")
                .append(req.getTotAmt()).append("|")
                .append(req.getMerinsid()).append("|")
                .append(req.getPayType()).append("|")
                .append(req.getBankNo()).append("|")
                .append(req.getRealName()).append("|")
                .append(req.getBankName()).append("|")
                .append(req.getPayFee()).append("|");

        String signStr = bufParam.toString().substring(0, bufParam.length() - 1);
        System.out.println(signStr);
        String sign = Sign.sign(signStr);
        req.setSign(sign);

        String xmlStr = req.createXml();

        System.out.println("+++++++"+xmlStr);
        return xmlStr;
    }


    public static void doDaifu() throws Exception {
        String xmlReq = init();
        System.out.println("------代付请求报文------\n" + xmlReq);

        try {
            InputStream is = HttpsUtils.doPost(IConstants.DAI_FU_URL, xmlReq);

            //转换返回结果
            String xmlRes = HttpsUtils.convertStreamToString(is);
            System.out.println("代付返回报文:" + xmlRes);
            xmlRes = xmlRes.substring(40, xmlRes.length() - 2);
            System.out.println("截取之后的代付返回报文:" + xmlRes);
            Y2e0010Res res = new Y2e0010Res();
            res.parseXml(xmlRes);

            System.out.println("rspCod:" + res.getRspcod() + "====rspMsg:" + res.getRspmsg());

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public static String genRondomNo(int num) {
        StringBuilder str = new StringBuilder();//定义变长字符串
        Random random = new Random();
        //随机生成数字，并添加到字符串
        for (int i = 0; i < num; i++) {
            str.append(random.nextInt(10));
        }
        //将字符串转换为数字并输出
        System.out.println(str.toString());
        return str.toString();
    }

    public static void main(String[] args) throws Exception {
        doDaifu();
    }
}

